package vn.sunnet.backviet01.game.coloa.util.mypool;

import java.util.Stack;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.pool.GenericPool;

import vn.sunnet.backviet01.game.coloa.AuLac.Arrow;

public class ArrowPool extends GenericPool<Arrow>{
	private TextureRegion mTextureRegion;
	
	public ArrowPool(TextureRegion pTextureRegion) {
		super();
		if (pTextureRegion == null) {
			throw new IllegalArgumentException("The texture region must not be NULL");
		} 
		
		mTextureRegion = pTextureRegion;
	}
	@Override
	protected Arrow onAllocatePoolItem() {
		// TODO Auto-generated method stub
		return new Arrow(0, 0, mTextureRegion.deepCopy());
	}

	public void resetTextureRegion(TextureRegion pTextureRegion) {
		mTextureRegion = pTextureRegion;
		Stack<Arrow> stack = this.getStack();
		Sprite temp;
		
		for (int i = 0; i < stack.size(); i++) {
			temp = stack.get(i);
			temp.updateTextureRegion(mTextureRegion, true);
		}
	}
	
	protected void onHandleRecycleItem(final Arrow _sprite) {
		_sprite.setIgnoreUpdate(true);
		_sprite.clearEntityModifiers();
		_sprite.clearUpdateHandlers();
		_sprite.detachChildren();
		_sprite.detachSelf();
		_sprite.resetInforArrow();
		_sprite.reset();
		
	}
	
	public int size() {
		return mAvailableItems.size();
	}
	
	protected void detackSelf() {
		int i = 0;
		while (i < getStack().size()) {
			Arrow arrow = getStack().get(i);
			if (arrow.detachSelf()) {
				getStack().remove(i);
				continue;
			}
			i++;
		}
	}
}
